package com.pxmeta.audio.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.audio.entity.Music;
import com.pxmeta.audio.entity.MusicUserLike;
import com.pxmeta.audio.mapper.MusicMapper;
import com.pxmeta.audio.mapper.MusicUserLikeMapper;
import com.pxmeta.audio.service.IMusicService;
import com.pxmeta.audio.service.IMusicUserLikeService;
import com.pxmeta.common.core.domain.AjaxResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class MusicUserLikeServiceImpl extends ServiceImpl<MusicUserLikeMapper, MusicUserLike> implements IMusicUserLikeService {

    private final IMusicService musicService;

    @Override
    public List<Music> getLikeMusicList(Long userId,Integer page, Integer size) {
        LambdaQueryWrapper<MusicUserLike> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MusicUserLike::getUserId, userId)
                .orderByDesc(MusicUserLike::getCreateTime);

        List<MusicUserLike> list = this.page(new Page<>(page, size)).getRecords();
        List<Long> musicIds = list.stream().map(MusicUserLike::getMusicId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(musicIds)){
            return Lists.newArrayList();
        }

        return musicService.listByIds(musicIds);
    }

    @Override
    public List<MusicUserLike> getByMusicIds(Long userId, List<Long> musicIds) {
        if (CollectionUtil.isEmpty(musicIds)){
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<MusicUserLike> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MusicUserLike::getUserId, userId)
                .in(MusicUserLike::getMusicId, musicIds);
        return this.list(wrapper);
    }
}
